# !/usr/bin/env python
# -*- coding: utf-8 -*-

import json
from json import JSONEncoder


class ClassEncoder(JSONEncoder):
    def default(self, obj):
        return obj.__dict__


class Schema:
    # 构造器
    def __init__(self, package: str, author: str, db: str) -> None:
        super().__init__()
        self.package = package
        self.author = author
        self.db = db

    def __iter__(self):
        yield from {
            "package": self.package,
            "author": self.author,
            "db": self.db
        }.items()

    # print输出
    def __str__(self):
        print("__str__")
        return json.dumps(dict(self), ensure_ascii=False, cls=ClassEncoder)

    def __repr__(self):
        # 如果__repr__被implement了，而__str__没有，那么后者等于前者
        # 普通打印使用__str__，但如果在字典里面，则会调用__repr__方法
        print("__repr__")
        return self.__str__()

    def concat(self, name: str):
        # 普通方法
        return self.package + "." + name

    @staticmethod
    def max(a: int, b: int):
        # 静态方法
        return a if a > b else b

    @staticmethod
    def max2(a: int, b=10):
        # 静态方法
        return a if a > b else b


if __name__ == '__main__':
    s = '{"package": "com", "author":"wind", "db": "test"}'
    schema = json.loads(s)
    print(schema, type(schema))

    schema2: Schema = json.loads(s)
    print(schema2, type(schema2))

    schema3 = Schema(schema['package'], schema['author'], schema['db'])
    print(schema3, type(schema3))
    d = {1: schema3}
    print(d)

    # 静态方法
    print(Schema.max(1, 3))
    print(Schema.max2(1))

